package school;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MathExam10 {

	private static PrintWriter writer = null; // 字符打印输出流
	private static BufferedReader reader = null;// 字符流
	static Random r = new Random();//随机数
	static int count = 0; // 题数
	static int x, y; // 运算数
	static String entry = null;//题目内容
	static String regex = "\\D+";//使用正则表达式，提取数字，进行运算
	static String route="D:/out10.txt";//文件路径
	static String grade ; // 年级
	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) throws FileNotFoundException {
		selectGrade(grade,count);
        answer();
	}

	//选择年级
	public static String selectGrade(String grade,int count) throws FileNotFoundException {
		while(true) {
			System.out.println("请选择一年级或二年级即1或2");
			grade = sc.nextLine();
			if(grade.isEmpty()) {
				System.out.println("空");
				continue;
			}
			Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
			Matcher isNum = pattern.matcher(grade);
			if (!isNum.matches()) {
				System.out.println("不是数字");
				continue;
			}
			if(Integer.parseInt(grade)==1) {
				oneGrade();
			}else if(Integer.parseInt(grade)==2) {
				twoGrade();
			}else {
				System.out.println("没有更高年级的，请重新输入");
				continue;
			}
			break;
		}
		return grade+","+count;
	}
	
	//生成一年级的题目
	public static int oneGrade() throws FileNotFoundException {
		while(true) {
			System.out.println("请输入题目");
			String countstr= sc.nextLine();
			if(countstr.isEmpty()) {
				System.out.println("空");
				continue;
			}
			Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
			Matcher isNum = pattern.matcher(countstr);
			if (!isNum.matches()) {
				System.out.println("不是数字");
				continue;
				}else {
				count=Integer.parseInt(countstr);
			}
			if(count<=0) {
				System.out.println("请输入大于0的题目数量!");
				continue;
			}
			break;
		}
		while (true) {			
			writer = new PrintWriter(new FileOutputStream(route));
			for (int i = 0; i < count; i++) {
				while (true) {
					x = r.nextInt(100); // 0-99
					y = r.nextInt(100); // 0-99
					if (x + y < 100) {
						break;
					}
				}
				// 加减算法的格式排布
				if (x >= y) {
					// 随机选一个算法
					if ((r.nextInt(2) + 1) % 2 == 0) {
						// 大的数值加小的数值也可能是同样大的数值相加
						entry = "(" + (i + 1) + ") " + x + " + " + y + " =";
					} else {
						// 大的数值减小的数值也可能是同样大的数值相减
						entry = "(" + (i + 1) + ") " + x + " - " + y + " =";
					}
				} else if (x < y) {
					// 随机选一个算法
					if ((r.nextInt(2) + 1) % 2 == 0) {
						// 小的数值加大的数值ֵ
						entry = "(" + (i + 1) + ") " + x + " + " + y + " =";
					} else {
						// 大的数值减小的数值（将大的放在前面）
						entry = "(" + (i + 1) + ") " + y + " - " + x + " =";
					}
				}
				writer.println(entry);
			}
			break;
		}
		writer.close();
		return count;
	}

	//生成二年级的题目
	private static int twoGrade() throws FileNotFoundException {
		while(true) {
			System.out.println("请输入题目");
			String countstr= sc.nextLine();
			if(countstr.isEmpty()) {
				System.out.println("空");
				continue;
			}
			Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
			Matcher isNum = pattern.matcher(countstr);
			if (!isNum.matches()) {
				System.out.println("不是数字");
				continue;
				}else {
				count=Integer.parseInt(countstr);
			}
			if(count<=0) {
				System.out.println("请输入大于0的题目数量!");
				continue;
			}
			break;
		}
		while(true) {
			writer = new PrintWriter(new FileOutputStream(route));
			for (int i = 0; i < count; i++) {
				x = 0;
				y = 0;
				entry = null;
				while (true) {
					x = r.nextInt(100) + 1; // 1-100
					y = r.nextInt(100) + 1; // 1-100
					if (x + y < 100) {
						if ((x * y < 100) || (x % y == 0)) {
							break;
						} else if (y % x == 0) {
							break;
						}
					}
				}
				// 乘除算法的格式排布
				if (x >= y) {
					// 随机选一个算法
					if ((r.nextInt(2) + 1) % 2 == 0) {
						if (x * y < 100) {
							entry = "(" + (i + 1) + ") " + x + " x " + y + " =";
						} else {
							entry = "(" + (i + 1) + ") " + x + " / " + y + " =";
						}
					} else {
						if (x % y == 0) {
							entry = "(" + (i + 1) + ") " + x + " / " + y + " =";
						} else {
							entry = "(" + (i + 1) + ") " + x + " x " + y + " =";
						}
					}
				} else if (x < y) {
					// 随机选一个算法
					if ((r.nextInt(2) + 1) % 2 == 0) {
						if (x * y < 100) {
							entry = "(" + (i + 1) + ") " + x + " x " + y + " =";
						} else {
							entry = "(" + (i + 1) + ") " + y + " / " + x + " =";
						}
					} else {
						if (y % x == 0) {
							entry = "(" + (i + 1) + ") " + y + " / " + x + " =";
						} else {
							entry = "(" + (i + 1) + ") " + x + " x " + y + " =";
						}
					}

				}
				writer.println(entry);
			}
			break;
		}
		writer.close();
		return count;
	}
	
	//生成out10.txt文件
	private static void answer() {
		String[] str = null;
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(route)));
			writer = new PrintWriter(new FileOutputStream(route, true));
			writer.println("----------标准答案----------");
			while ((entry = reader.readLine()) != null) {
				str = entry.split(regex);
				int arr[] = new int[str.length - 1];
				for (int i = 0; i < str.length - 1; i++) {
					arr[i] = Integer.parseInt(str[i + 1]);
				}
				if (entry.indexOf("+") > 0) {
					writer.println(entry + " " + (arr[1] + arr[2]));
				} else if (entry.indexOf("-") > 0) {
					writer.println(entry + " " + (arr[1] - arr[2]));
				} else if (entry.indexOf("x") > 0) {
					writer.println(entry + " " + (arr[1] * arr[2]));
				} else if (entry.indexOf("/") > 0) {
					writer.println(entry + " " + (arr[1] / arr[2]));
				}

				str = null;
				entry = null;
			}
			SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
			writer.println("\t第10组 " + df.format(new Date()));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}
